/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.sensor.cropcircle;


import nl.cloudfarming.client.sensor.MalformedFileFormatException;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;
import nl.cloudfarming.client.model.SensorDataFile;
import nl.cloudfarming.client.model.SensorMetadata;
import static org.junit.Assert.*;
import org.junit.Test;

/**
 *
 * @author Merijn Zengers
 */
public class CropcircleFileProcessorIT {

    static final File CROPCIRCLE_SENSORFILE_SUCCESS = new File("./src/test/resources/130111CropcircleProcessSuccessTest.CSV");
    static final File CROPCIRCLE_SENSORFILE_FAIL = new File("./src/test/resources/130111CropcircleProcessSuccessTest.CSV");

    //MakeDateFromFileName()
    @Test
    public void makeDateFromFileNameTest() throws ParseException, java.text.ParseException,  MalformedFileFormatException, IOException{
        //Setup

        CropcirclePreference.SENSOR_DATA_FILE_FORMAT.saveValue("DDMMYYCC.CSV");
        String filename = "140110AB.csv";
        File f = File.createTempFile(filename,".csv");
        CropcircleFileProcessor processor =  new CropcircleASC340FileProcessor(File.createTempFile(filename,".csv"));


        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(2010, 00, 14, 00 ,00 ,00 ); // Month starts at 0 = january
        calendar.getTime().toString();

        Date returnDate = processor.getDateFromFileName(f);

        //test
        //In gregorian calendar the miliseconds can't be set. so must divide everything by 1000
        assertEquals(returnDate.getTime()/1000,calendar.getTimeInMillis()/1000);
    }

    @Test
    public void buildMetdataFromPreferencesTest() throws IOException{
        //Setup

        CropcirclePreference.SENSOR_DATA_FILE_FORMAT.saveValue("DDMMYYCC.CSV");
        CropcircleFileProcessor processor =  new CropcircleASC340FileProcessor(File.createTempFile("cropcircle",".csv"));
        //Test

        assertEquals("DDMMYYCC.CSV", ((SensorMetadata)processor.buildMetadataFromPreferences()).getDateFormat());
    }

    //MakeDateFromFileName
    @Test (expected = AssertionError.class)
    public void EmptyFileFormatPropertyTest() throws ParseException, java.text.ParseException, MalformedFileFormatException, IOException{
        //Setup

        CropcirclePreference.SENSOR_DATA_FILE_FORMAT.saveValue("");
        String filename = "140110AB.csv";
        File f = File.createTempFile(filename,".csv");
        CropcircleFileProcessor processor =  new CropcircleASC340FileProcessor(File.createTempFile(filename,".csv"));
        //test
        Date returnDate = processor.getDateFromFileName(f);
    }

    //MakeDateFromFileName
    @Test (expected = MalformedFileFormatException.class)
    public void MalformedFileFormatExceptionTest() throws ParseException, java.text.ParseException,  MalformedFileFormatException,  IOException{
        //Setup
        CropcirclePreference.SENSOR_DATA_FILE_FORMAT.saveValue("DD");
        String filename = "140110AB.csv";
        File f = File.createTempFile(filename,".csv");
        CropcircleFileProcessor processor =  new CropcircleASC340FileProcessor(File.createTempFile(filename,".csv"));
        //test
        Date returnDate = processor.getDateFromFileName(f);
    }

    //MakeDateFromFileName
    @Test (expected = ParseException.class)
    public void ParseExceptionTest() throws ParseException, java.text.ParseException,  MalformedFileFormatException, IOException{
        //Setup

        CropcirclePreference.SENSOR_DATA_FILE_FORMAT.saveValue("DDMMYYCC.CSV");
        String filename = "dd++__++()sdAB";
        File f = File.createTempFile(filename,".csv");
        CropcircleFileProcessor processor =  new CropcircleASC340FileProcessor(File.createTempFile(filename,".csv"));
        //test
        Date returnDate = processor.getDateFromFileName(f);
    }

    // Parsing is covered by this test as well
    @Test
    public void parseTest() throws  IOException, ParseException, CropcircleParseException{
        //Setup
        CropcircleASC340FileProcessor processor =  new CropcircleASC340FileProcessor(File.createTempFile("cropcircle",".csv"));
        SensorMetadata metadata = new SensorMetadata("DDMMYY", 350, 2, "234,246", "ACS430", "670,730,780");
        SensorDataFile dataFile = new SensorDataFile("fileName", "checksum", new Date(), new Date(), metadata);
        //Test
        CropcircleSensorData sensorData = processor.parse("0003.8510790,0051.5193047,4,042352.06,0.0,175.8,4,-0.039,-0.026,25.5060,23.5890,24.8690", dataFile);
        //Validate
        assertEquals(51.5193047,sensorData.getLatitude(),0);
    }
    
    // Parsing is covered by this test as well
    @Test (expected = CropcircleParseException.class)
    public void parseDateTest() throws  IOException, ParseException, CropcircleParseException{
        //Setup
        CropcircleFileProcessor processor =  new CropcircleASC340FileProcessor(File.createTempFile("cropcircle",".csv"));
        SensorMetadata metadata = new SensorMetadata("DDMMYY", 350, 2, "234,246", "ACS430", "670,730,780");
        SensorDataFile dataFile = new SensorDataFile("fileName", "checksum", new Date(), new Date(), metadata);
        //Test
        CropcircleSensorData sensorData = processor.parse("0003.8510790,0051.5193047,4,+++_)(,0.0,175.8,4,-0.039,-0.026,25.5060,23.5890,24.8690", dataFile);
        //Validate
        assertEquals(51.5193047,sensorData.getLatitude(),0);
    }
    
    // Parsing is covered by this test as well
    @Test
    public void processSuccesTest() throws IOException{
        assertTrue(CROPCIRCLE_SENSORFILE_SUCCESS.exists());

        CropcircleFileProcessor processor =  new CropcircleASC340FileProcessor(CROPCIRCLE_SENSORFILE_SUCCESS);
        processor.process();
    }



}
